package com.dapeis.api.controller.collocation.impl;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

import com.ag.core.base.Pager;
import com.ag.core.exception.SysTipsException;
import com.ag.core.utils.AgAssert;
import com.ag.utils.NumberUtils;
import com.ag.utils.json.AGsonBuilder;
import com.dapeis.api.controller.RestController;
import com.dapeis.api.controller.collocation.ITaskController;
import com.dapeis.core.convert.pager.OrderPagerSearchConvertCriteria;
import com.dapeis.core.utils.DBConfigUtils;
import com.dapeis.core.utils.StylistConstants;
import com.dapeis.domain.Order;
import com.dapeis.domain.User;
import com.dapeis.domain.ext.OrderExt;
import com.dapeis.domain.ext.TaskOrderExt;
import com.dapeis.domain.ext.UserInformationExt;
import com.dapeis.service.MoneyLogService;
import com.dapeis.service.OrderService;
import com.dapeis.service.TIMService;
import com.dapeis.service.TaskService;
import com.dapeis.service.UserInformationService;
import com.dapeis.service.order.OrderFlow;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;

/**
 * @author 作者 E-mail: hzyuanyong@126.com
 * @version 创建时间：2017年5月15日 下午3:07:19 类说明
 */
@Controller
@RequestMapping("/dps/task")
public class TaskImpController extends RestController implements
	ITaskController {

    @Resource
    TaskService taskService;

    @Resource
    OrderService orderService;

    @Resource
    UserInformationService userInformationService;

    @Override
    public JsonElement taskList(
	    @RequestParam(required = false) Integer curretPager,
	    HttpServletRequest request) {
	Pager pager = new Pager();
	pager.init(OrderExt.class, new OrderPagerSearchConvertCriteria());
	Example example = new Example(TaskOrderExt.class);
	Criteria criteria = example.createCriteria();
	criteria.andEqualTo(Order.PROPERTY_STATE,
		StylistConstants.Order.Status.PAY_SUCCESS);
	criteria.andEqualTo(Order.PROPERTY_PAYSTATE,
		StylistConstants.Order.PayStatus.PAY_SUCCESS);
	criteria.andEqualTo(Order.PROPERTY_TYPE,
		StylistConstants.Order.Type.REQUIREMENT);
	pager.setExample(example);
	if (NumberUtils.isValid(curretPager))
	    pager.setCurrentPage(curretPager);
	

	taskService.pager(pager);
	JsonObject json = buildSuccess();
	json.add("pager", AGsonBuilder.create().toJsonTree(pager));
	return json;

    }

    @Override
    public JsonElement taskDetail(@RequestParam Long orderId,
	    HttpServletRequest request) {
	TaskOrderExt taskOrderExt = taskService.selectByPrimaryKey(orderId);
	AgAssert.notNull(taskOrderExt, SysTipsException.class, "查找不到");
	OrderFlow orderFlow = orderService.getFlow(taskOrderExt.getType());
	orderFlow
		.check(taskOrderExt, StylistConstants.Order.Status.PAY_SUCCESS);
	// 此处因为搭配师API需要解析成所有对应的值,重新生成一个类
	UserInformationExt userInformationExt = null;
	if (null != taskOrderExt.getUserExt().getUserInformation()) {
	    userInformationExt = new UserInformationExt();
	    // 个人信息是选填
	    BeanUtils.copyProperties(taskOrderExt.getUserExt()
		    .getUserInformation(), userInformationExt);
	}
	JsonObject json = buildSuccess();
	json.add("requiremnt",
		AGsonBuilder.create().toJsonTree(taskOrderExt.getRequirement()));
	json.add(
		"user",
		AGsonBuilder.create()
			.toJsonTree(taskOrderExt.getUserExt(), User.class)
			.getAsJsonObject());
	json.add("userInfo",
		AGsonBuilder.create().toJsonTree(userInformationExt));
	JsonObject ordObject = new JsonObject();
	json.addProperty("orderId", taskOrderExt.getId().toString());
	json.addProperty("time",
		DBConfigUtils.unixTime2ago(taskOrderExt.getPayTime()));
	return json;
    }

    @Override
    public JsonElement task(@RequestParam Long orderId,
	    HttpServletRequest request) {
	Order order = orderService.selectByPrimaryKey(orderId);
	AgAssert.notNull(order, SysTipsException.class, "查询不到订单信息:" + orderId);
	OrderFlow orderFlow = orderService.getFlow(order.getType());
	orderFlow.check(order, StylistConstants.Order.Status.PAY_SUCCESS);
	orderService.task(orderId, getThreadLocalCollocationId());
	JsonObject json = buildSuccess();
	return json;
    }

    @Override
    public JsonElement finishService(@RequestParam Long orderId) {
	orderService.service(orderId, getThreadLocalCollocationId(), null);
	JsonObject json = buildSuccess();
	return json;
    }

}
